package pa1.sockets;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {


	public static void main(String[] args) {
		Socket socket;
		int nrEvents;
		String clientName;
		BufferedReader in;
		PrintWriter out;
		Scanner input = new Scanner(System.in);
		
		try {
			//initialise the socket
			socket = new Socket("127.0.0.1", 4444);
			
			if(socket != null){
				//get in- and outputstream from the socket in order to send and receive data
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				out = new PrintWriter(socket.getOutputStream(), true);
				
				//process user input
				
				//client name
				System.out.println("Enter your name:");
				clientName = input.nextLine();
				
				out.println("HELO " + clientName);
				System.out.println(in.readLine());	//server response
				
				//nr of events
				System.out.println("Enter the number of events:");
				nrEvents = Integer.parseInt(input.nextLine());
				
				out.println("NREV " + nrEvents);
				System.out.println(in.readLine());	//server response
				
				//enter all events
				for(int i = 0; i < nrEvents; i++){
					System.out.println("Enter next event:");
					out.println("EVNT " + input.nextLine());
				}
				
				out.println("QUIT");
				System.out.println(in.readLine());

				//close in- and output streams and the connection
				in.close();
				out.close();
				socket.close();
				input.close();
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		
		

	}

}
